package com.application.meizhi.persenter.douban;

import android.content.Context;

import com.application.meizhi.base.BasePresenter;

import com.application.meizhi.http.RetrofitHelper;
import com.application.meizhi.model.douban.DouBanMeiZhi;
import com.application.meizhi.ui.iView.douban.IDouBanSingleView;

import com.application.meizhi.uitl.MeiZhiUtils;
import com.orhanobut.logger.Logger;


import java.util.List;


import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by gskl on 16/10/12.
 */

public class DouBanSinglePresenter extends BasePresenter<IDouBanSingleView> {
    public DouBanSinglePresenter(Context context, IDouBanSingleView iView) {
        super(context, iView);
    }

    @Override
    public void release() {
        if (subscription != null) {
            subscription.unsubscribe();
        }
    }

    public void getDouBan(String type, int page) {
        RetrofitHelper.getDoubanMeiziApi().getDoubanMeizi(type, page).enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                List<DouBanMeiZhi> list = MeiZhiUtils.parserDouBanListHtml(type, response);
                if (list.size() > 0) {
                    iView.showDouBanList(list);
                } else {
                    iView.noList();
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                Logger.d("onFailure");

            }
        });
    }
}
